<template>
  <div class="container">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      class="subForm"
    >
      <el-form
        :model="numberValidateForm"
        ref="numberValidateForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="学科名称"
          prop="text"
          :rules="[{ required: true, message: '内容不能为空' }]"
        >
          <el-input
            @change="submit('numberValidateForm')"
            type="text"
            v-model.trim="numberValidateForm.text"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-switch
            class="subBtn"
            v-model="value"
            inactive-text="是否显示"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeBtn"
          >
          </el-switch>
        </el-form-item>
        <el-form-item class="footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submit('numberValidateForm')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from "../../api/hmmm/subjects";
export default {
  name: "Add",
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      required: true
    },
    nowSub: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      value: true,
      numberValidateForm: {
        text: ""
      }
    };
  },
  created() {
    this.loadSub();
  },
  methods: {
    handleClose() {
      this.$emit("changes");
    },
    async submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.title === "新增学科") {
            let value = this.value === true ? 1 : 0;
            add({
              subjectName: this.numberValidateForm.text,
              isFrontDisplay: value
            });
            this.$emit("addSub", this.numberValidateForm);
            this.listLoading = false;
          } else {
            let value = this.value === true ? 1 : 0;
            update({
              id: this.nowSub.id,
              subjectName: this.numberValidateForm.text,
              isFrontDisplay: value
            });
            this.$emit("changeSubs", this.numberValidateForm);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    changeBtn(value) {
      this.value = value;
    },
    loadSub() {
      if (this.title === "新增学科") {
        this.value = true;
      } else {
        this.numberValidateForm.text = this.nowSub.subjectName;
        let subValue = this.nowSub.isFrontDisplay == 1 ? true : false;
        this.value = subValue;
      }
    }
  }
};
</script>

<style scoped lang="scss">
.container {
  .subForm {
    height: 1000px;
  }
  .subBtn {
    margin-left: -80px;
  }
  .footer {
    margin-left: 129px;
    margin-top: 50px;
    margin-bottom: 0;
  }
  .el-button {
    display: inline-block;
  }
  .el-switch {
    ::v-deep .el-switch__label {
      margin-left: 15px;
      font-weight: 700;
      color: #606266;
    }
  }
}
</style>
